# Based on the file created for Arch Linux by:
# Tobias Powalowski <tpowa@archlinux.org>
# Thomas Baechler <thomas@archlinux.org>

# Maintainer : Philip Müller <philm@manjaro.org>
# Maintainer : Rob McCathie <rob@manjaro.org>

pkgbase=linux312
pkgname=('linux312' 'linux312-headers')
_kernelname=-MANJARO
_basekernel=3.12
_basever=312
_aufs=20140623
_bfq=v7r5
pkgver=${_basekernel}.24
pkgrel=1
arch=('i686' 'x86_64')
url="http://www.kernel.org/"
license=('GPL2')
makedepends=('xmlto' 'docbook-xsl' 'kmod' 'inetutils' 'bc')
options=('!strip')
source=("http://www.kernel.org/pub/linux/kernel/v3.x/linux-${_basekernel}.tar.xz"
        "http://www.kernel.org/pub/linux/kernel/v3.x/patch-${pkgver}.xz"
        # the main kernel config files
        'config' 'config.x86_64' 'config.aufs'
        # standard config files for mkinitcpio ramdisk
        "${pkgname}.preset"
        "aufs3.12-${_aufs}.patch.bz2"
        'aufs3-base.patch'
        'aufs3-kbuild.patch'
        'aufs3-loopback.patch'
        'aufs3-mmap.patch'
        'aufs3-standalone.patch'
        'tmpfs-ibitmap.patch'
        'vfs-ino.patch'
        'change-default-console-loglevel.patch'
        'http://sources.gentoo.org/cgi-bin/viewvc.cgi/linux-patches/genpatches-2.6/trunk/3.12/1700_enable-thinkpad-micled.patch'
        'http://sources.gentoo.org/cgi-bin/viewvc.cgi/linux-patches/genpatches-2.6/trunk/3.12/2700_ThinkPad-30-brightness-control-fix.patch'
        'criu-no-expert.patch'
        # AMD fixes
        '1-4-ramdisk-Export_relocated_ramdisk_VA.patch::https://lkml.org/lkml/diff/2013/12/5/621/1'
        '2-4-microcode-Share_native_MSR_accessing_variants.patch::https://lkml.org/lkml/diff/2013/12/5/620/1'
        '3-4-microcode-AMD-Fix_early_ucode_loading.patch::https://lkml.org/lkml/diff/2013/12/5/626/1'
        '4-4-microcode-Move_to_a_proper_location.patch::https://lkml.org/lkml/diff/2013/12/5/698/1'
        'nfs-check-gssd-running-before-krb5i-auth.patch'
        'rpc_pipe-fix-cleanup-of-dummy-gssd-directory-when-notification-fails.patch'
        'rpc_pipe-remove-the-clntXX-dir-if-creating-the-pipe-fails.patch'
        'sunrpc-add-an-info-file-for-the-dummy-gssd-pipe.patch'
        'sunrpc-create-a-new-dummy-pipe-for-gssd-to-hold-open.patch'
        'sunrpc-replace-gssd_running-with-more-reliable-check.patch'
        "0001-block-cgroups-kconfig-build-bits-for-BFQ-${_bfq}.patch::http://algo.ing.unimo.it/people/paolo/disk_sched/patches/3.12.0-${_bfq}/0001-block-cgroups-kconfig-build-bits-for-BFQ-${_bfq}-3.12.patch"
        "0002-block-introduce-the-BFQ-${_bfq}-I-O-sched.patch::http://algo.ing.unimo.it/people/paolo/disk_sched/patches/3.12.0-${_bfq}/0002-block-introduce-the-BFQ-${_bfq}-I-O-sched-for-3.12.patch"
        "0003-block-bfq-add-Early-Queue-Merge-EQM-to-BFQ-${_bfq}.patch::http://algo.ing.unimo.it/people/paolo/disk_sched/patches/3.12.0-${_bfq}/0003-block-bfq-add-Early-Queue-Merge-EQM-to-BFQ-${_bfq}-for-3.12.0.patch"
)
sha256sums=('2e120ec7fde19fa51dc6b6cc11c81860a0775defcad5a5bf910ed9a50e845a02'
            '7eee42d670106996e5258e0b61b604ee85c915f995fae6d664a2a19394420c14'
            'ef742f7ba913e75d31d3946ba80dbe831b83b9b5c36d79e235c650f5620195ed'
            '8813b088a0c73b8d4ecc751dbe2a9b1af7aba69ea9ab1371fdf2f67bd2f249d8'
            'b1b1cf291bd68270b700eb21f19f7b052116d07dde492761227cd94bb775910c'
            'd3be8e800b7fa0ab82700a61f7c85e4fe351f1b73688120a9a465bd34b473b19'
            '6bc2bf4108619cbdb154cad468cba6c200c778469e7caf744c3078e1af4e83d7'
            '03ec8d29245b222c21aee0a043ab9a48702d252d8460d44541fa9ea65bcea7ce'
            'e9604c6d87ae87830c154c1b12065eb6361e503006025497741f15dbaa9fd3ff'
            'd84621d306f7a6931b5f42061a21e99899ceedfee7c0e5f51a272851a5a95b63'
            '3205cf2a7b48fd03840c16de434d79b99ebc7604f848e5d05054e7426525d014'
            '90da6391dfc74124be8fb4144dc2f5fbd976a9da4241bba17b0482d145a0252c'
            '173ede3f735979812010e52a2ad5c9ac326621f3993a9cebf4a2e4c089f035b4'
            '3979d348824f61e42587939e82b41e47668bb068b47ad08adb92ebbd717622a7'
            'd633a2039877c00302f32738099ee53e0275350a8f73691ecb107fb9735db5a9'
            '461aa0da7de8bda9474797339532304894b55825be34f8c009244da8c00c5b41'
            'a3f85c3c35ee478fd174f8aaa6ca15e5fad8612b42ca4d90cc3ef79b49a99989'
            'daa75228a4c45a925cc5dbfeba884aa696a973a26af7695adc198c396474cbd5'
            '0bde8ac782976b7ffd029cd8bf924447730bf4efdd6eddf5b3cd98a074f15fa3'
            '8221a084ebdaa506d7f8f2864b383d2d4403ead9f6aedafcb683e287a517f831'
            'd16446fa063490b17e8a656ac5b4ecce35a947112d7d78b29997757cea51e1fe'
            '55fb9010e1bcecb2300b0f13aa47b341d69d23d964ddb74f48e4139539ec52f7'
            '139d576c540840fe18b0ce8468c243011e5f8b4aebfb1a288afe10b9f8d88137'
            'fecca9a761f3427c7f2f793e5d9a34d6f43799143e9a6b51edc372f88c76022e'
            'c045ce9c2571c4e6ff63345ea9abd9f778f56206f686b95491523ac740458420'
            'd8d6745a165e7f1608d8c298e49c8c2fee5a5d6c61c5b80d63a8dc7913ff8dc8'
            'b6f366ed2100ccab99029f61c39c0781ad4863f537381626196ed43b4b115a21'
            'cbfabd113a1954cd3d6cc1e70c19b95ce36a19e2c92ad93939d4982077178135'
            '6c3b1030c3866c371a8cd3b07427be0d62ed62a8ec74ccefb2f6be2909bd0773'
            'f3c7c5ad39d58b6faa4f2decf3bb7ee1dd8b2f32856abe00192d0c41401a8b57'
            '217745a7ffbcdae967e22fe74991f9808ad2b4dd840d8c5d6683e617ea961968'
)

## modules.symboles md5sums
#
# i686
# 327f520435aac3162c637fba46c7b6f4  /lib/modules/3.10.27-1-MANJARO/modules.symbols
# ae67bd3f9ee2fbe29dddf2cd7786eccc  /lib/modules/3.11.10.3-1-MANJARO/modules.symbols
# ba15d7a09322f0ecb0477506caa190d9  /lib/modules/3.12.8-1-MANJARO/modules.symbols
# 8d470593b37f21822a5fc2dda4d5362b  /lib/modules/3.13.0-1-MANJARO/modules.symbols
# 7b296a2ce5395f35a3c06b4d2ea8fb39  /lib/modules/3.4.77-1-MANJARO/modules.symbols
# d883c826d56325c13816a4ea3395575d  /lib/modules/3.8.13.16-1-MANJARO/modules.symbols
# x86_64
# 9c82210478776c06df877d97e10ed7f0  /lib/modules/3.10.27-1-MANJARO/modules.symbols
# ca2dd8e6c02fed078b06c7e947654890  /lib/modules/3.11.10.3-1-MANJARO/modules.symbols
# 376919bb83a21900eb1fd90d852daaf5  /lib/modules/3.12.8-1-MANJARO/modules.symbols
# cfdd0ca8d49e3c908ba7c1de8e3cbc8d  /lib/modules/3.13.0-1-MANJARO/modules.symbols
# 0d85eb4571ce79a5df3015bcf714e46a  /lib/modules/3.4.77-1-MANJARO/modules.symbols
# b11bfd0f71f687adcad99fbbc820a517  /lib/modules/3.8.13.16-1-MANJARO/modules.symbols

prepare() {
  cd "${srcdir}/linux-${_basekernel}"

  time make mrproper

  mkdir -p firmware/{amd-ucode,rtl-nic}

  # add upstream patch
  patch -p1 -i "${srcdir}/patch-${pkgver}"

  # add latest fixes from stable queue, if needed
  # http://git.kernel.org/?p=linux/kernel/git/stable/stable-queue.git
  # enable only if you have "gen-stable-queue-patch.sh" executed before
  #patch -Np1 -i "${srcdir}/prepatch-${_basekernel}`date +%Y%m%d`"

  # set DEFAULT_CONSOLE_LOGLEVEL to 4 (same value as the 'quiet' kernel param)
  # remove this when a Kconfig knob is made available by upstream
  # (relevant patch sent upstream: https://lkml.org/lkml/2011/7/26/227)
  patch -Np1 -i "${srcdir}/change-default-console-loglevel.patch"

  # allow criu without expert option set
  # patch from fedora
  patch -Np1 -i "${srcdir}/criu-no-expert.patch"

  # fix 15 seocnds nfs delay
  patch -Np1 -i "${srcdir}/sunrpc-create-a-new-dummy-pipe-for-gssd-to-hold-open.patch"
  patch -Np1 -i "${srcdir}/sunrpc-replace-gssd_running-with-more-reliable-check.patch"
  patch -Np1 -i "${srcdir}/nfs-check-gssd-running-before-krb5i-auth.patch"
  # fix nfs kernel oops
  # #37866
  patch -Np1 -i "${srcdir}/rpc_pipe-remove-the-clntXX-dir-if-creating-the-pipe-fails.patch"
  patch -Np1 -i "${srcdir}/sunrpc-add-an-info-file-for-the-dummy-gssd-pipe.patch"

  patch -Np1 -i "${srcdir}/rpc_pipe-fix-cleanup-of-dummy-gssd-directory-when-notification-fails.patch"

  # add Gentoo patches
  patch -Np1 -i "${srcdir}/1700_enable-thinkpad-micled.patch"
  patch -Np1 -i "${srcdir}/2700_ThinkPad-30-brightness-control-fix.patch"

  # add AMD fixes
  patch -Np1 -i "${srcdir}/1-4-ramdisk-Export_relocated_ramdisk_VA.patch"
  patch -Np1 -i "${srcdir}/2-4-microcode-Share_native_MSR_accessing_variants.patch"
  patch -Np1 -i "${srcdir}/3-4-microcode-AMD-Fix_early_ucode_loading.patch"
  patch -Np1 -i "${srcdir}/4-4-microcode-Move_to_a_proper_location.patch"

  # add aufs3 support
  patch -Np1 -i "${srcdir}/aufs3.12-${_aufs}.patch"
  patch -Np1 -i "${srcdir}/aufs3-base.patch"
  patch -Np1 -i "${srcdir}/aufs3-kbuild.patch"
  patch -Np1 -i "${srcdir}/aufs3-loopback.patch"
  patch -Np1 -i "${srcdir}/aufs3-mmap.patch"
  patch -Np1 -i "${srcdir}/aufs3-standalone.patch"
  patch -Np1 -i "${srcdir}/tmpfs-ibitmap.patch"
  patch -Np1 -i "${srcdir}/vfs-ino.patch"

  # add BFQ scheduler
  patch -Np1 -i "${srcdir}/0001-block-cgroups-kconfig-build-bits-for-BFQ-${_bfq}.patch"
  patch -Np1 -i "${srcdir}/0002-block-introduce-the-BFQ-${_bfq}-I-O-sched.patch"
  patch -Np1 -i "${srcdir}/0003-block-bfq-add-Early-Queue-Merge-EQM-to-BFQ-${_bfq}.patch"

#  if [ "${CARCH}" = "x86_64" ]; then
#    cat "${srcdir}/config.x86_64" > ./.config
#  else
#    cat "${srcdir}/config" > ./.config
#  fi
  cp "/home/emacs/build/packages-core/linux312/makepkg/linux312/src/linux-3.12/.config" ./.config

  time make nconfig

#  cat "${srcdir}/config.aufs" >> ./.config

#  if [ "${_kernelname}" != "" ]; then
#    sed -i "s|CONFIG_LOCALVERSION=.*|CONFIG_LOCALVERSION=\"${_kernelname}\"|g" ./.config
#  fi

  # set extraversion to pkgrel
#  sed -ri "s|^(EXTRAVERSION =).*|\1 -${pkgrel}|" Makefile

  # don't run depmod on 'make install'. We'll do this ourselves in packaging
  sed -i '2iexit 0' scripts/depmod.sh

  # normally not needed
  make clean

  # get kernel version
  make prepare

  # run scripts
  make scripts

}

build() {
  cd "${srcdir}/linux-${_basekernel}"

  # load configuration
  # Configure the kernel. Replace the line below with one of your choice.
  #make menuconfig # CLI menu for configuration
  #time make nconfig # new CLI menu for configuration
  #make xconfig # X-based configuration
  #make oldconfig # using old config from previous kernel version
  # ... or manually edit .config

  ####################
  # stop here
  # this is useful to configure the kernel
  #msg "Stopping build"
  #return 1
  ####################

  #yes "" | make config

  # build!
  make ${MAKEFLAGS} bzImage modules
}

package_linux312() {
  pkgdesc="The ${pkgbase/linux/Linux} kernel and modules"
  depends=('coreutils' 'linux-firmware' 'kmod' 'mkinitcpio>=0.7' 'cpio')
  optdepends=('crda: to set the correct wireless channels of your country')
  provides=("linux=${pkgver}")
  install=${pkgname}.install

  cd "${srcdir}/linux-${_basekernel}"

  KARCH=x86

  # get kernel version
  _kernver="$(make kernelrelease)"

  mkdir -p "${pkgdir}"/{lib/modules,lib/firmware,boot}
  make INSTALL_MOD_PATH="${pkgdir}" modules_install
  cp arch/$KARCH/boot/bzImage "${pkgdir}/boot/vmlinuz-${_basever}-${CARCH}"

  # add kernel version
  if [ "${CARCH}" = "x86_64" ]; then
     echo "${pkgver}-${pkgrel}-MANJARO x64" > "${pkgdir}/boot/${pkgbase}-${CARCH}.kver"
  else
     echo "${pkgver}-${pkgrel}-MANJARO x32" > "${pkgdir}/boot/${pkgbase}-${CARCH}.kver"
  fi

  # add vmlinux
  install -D -m644 vmlinux "${pkgdir}/usr/src/linux-${_kernver}/vmlinux"

  # install fallback mkinitcpio.conf file and preset file for kernel
  install -D -m644 "${srcdir}/${pkgname}.preset" "${pkgdir}/etc/mkinitcpio.d/${pkgname}.preset"

  # set correct depmod command for install
  sed \
    -e  "s/RELEASE=.*/RELEASE=${pkgver}-${pkgrel}/g" \
    -e  "s/KERNEL_VERSION=.*/KERNEL_VERSION=${_kernver}/g" \
    -e  "s/KERNEL_BASE=.*/KERNEL_BASE=${_basekernel}/g" \
    -e  "s/BASEVER=.*/BASEVER=${_basever}/g" \
    -e  "s/ARCH=.*/ARCH=${CARCH}/g" \
    -i "${startdir}/${pkgname}.install"
  sed \
    -e "s|ALL_kver=.*|ALL_kver=\"/boot/vmlinuz-${_basever}-${CARCH}\"|g" \
    -e "s|default_image=.*|default_image=\"/boot/initramfs-${_basever}-${CARCH}.img\"|g" \
    -e "s|fallback_image=.*|fallback_image=\"/boot/initramfs-${_basever}-${CARCH}-fallback.img\"|g" \
    -i "${pkgdir}/etc/mkinitcpio.d/${pkgname}.preset"

  # remove build and source links
  rm -f "${pkgdir}"/lib/modules/${_kernver}/{source,build}
  # remove the firmware
  rm -rf "${pkgdir}/lib/firmware"
  # gzip -9 all modules to save 100MB of space
  find "${pkgdir}" -name '*.ko' -exec gzip -9 {} \;
  # make room for external modules
  ln -s "../extramodules-${_basekernel}${_kernelname:--MANJARO}" "${pkgdir}/lib/modules/${_kernver}/extramodules"
  # add real version for building modules and running depmod from post_install/upgrade
  mkdir -p "${pkgdir}/lib/modules/extramodules-${_basekernel}${_kernelname:--MANJARO}"
  echo "${_kernver}" > "${pkgdir}/lib/modules/extramodules-${_basekernel}${_kernelname:--MANJARO}/version"

  # Now we call depmod...
  depmod -b "$pkgdir" -F System.map "$_kernver"

  # move module tree /lib -> /usr/lib
  mv "$pkgdir/lib" "$pkgdir/usr"
}

package_linux312-headers() {
  pkgdesc="Header files and scripts for building modules for ${pkgbase/linux/Linux} kernel"
  provides=("linux-headers=$pkgver")

  install -dm755 "${pkgdir}/usr/lib/modules/${_kernver}"

  cd "${pkgdir}/usr/lib/modules/${_kernver}"
  ln -sf ../../../src/linux-${_kernver} build

  cd "${srcdir}/linux-${_basekernel}"

  install -D -m644 Makefile \
    "${pkgdir}/usr/src/linux-${_kernver}/Makefile"
  install -D -m644 kernel/Makefile \
    "${pkgdir}/usr/src/linux-${_kernver}/kernel/Makefile"
  install -D -m644 .config \
    "${pkgdir}/usr/src/linux-${_kernver}/.config"

  mkdir -p "${pkgdir}/usr/src/linux-${_kernver}/include"

  for i in acpi asm-generic config crypto drm generated linux math-emu \
    media net pcmcia scsi sound trace uapi video xen; do
    cp -a include/${i} "${pkgdir}/usr/src/linux-${_kernver}/include/"
  done

  # copy arch includes for external modules
  mkdir -p "${pkgdir}/usr/src/linux-${_kernver}/arch/x86"
  cp -a arch/x86/include "${pkgdir}/usr/src/linux-${_kernver}/arch/x86/"

  # copy files necessary for later builds, like nvidia and vmware
  cp Module.symvers "${pkgdir}/usr/src/linux-${_kernver}"
  cp -a scripts "${pkgdir}/usr/src/linux-${_kernver}"

  # fix permissions on scripts dir
  chmod og-w -R "${pkgdir}/usr/src/linux-${_kernver}/scripts"
  mkdir -p "${pkgdir}/usr/src/linux-${_kernver}/.tmp_versions"

  mkdir -p "${pkgdir}/usr/src/linux-${_kernver}/arch/${KARCH}/kernel"

  cp arch/${KARCH}/Makefile "${pkgdir}/usr/src/linux-${_kernver}/arch/${KARCH}/"

  if [ "${CARCH}" = "i686" ]; then
    cp arch/${KARCH}/Makefile_32.cpu "${pkgdir}/usr/src/linux-${_kernver}/arch/${KARCH}/"
  fi

  cp arch/${KARCH}/kernel/asm-offsets.s "${pkgdir}/usr/src/linux-${_kernver}/arch/${KARCH}/kernel/"

  # add headers for lirc package
  # pci
  for i in bt8xx cx88 saa7134; do
    mkdir -p "${pkgdir}/usr/src/linux-${_kernver}/drivers/media/pci/${i}"
    cp -a drivers/media/pci/${i}/*.h "${pkgdir}/usr/src/linux-${_kernver}/drivers/media/pci/${i}"
  done
  # usb
  for i in cpia2 em28xx pwc sn9c102; do
    mkdir -p "${pkgdir}/usr/src/linux-${_kernver}/drivers/media/usb/${i}"
    cp -a drivers/media/usb/${i}/*.h "${pkgdir}/usr/src/linux-${_kernver}/drivers/media/usb/${i}"
  done
  # i2c
  mkdir -p "${pkgdir}/usr/src/linux-${_kernver}/drivers/media/i2c"
  cp drivers/media/i2c/*.h  "${pkgdir}/usr/src/linux-${_kernver}/drivers/media/i2c/"
  for i in cx25840; do
    mkdir -p "${pkgdir}/usr/src/linux-${_kernver}/drivers/media/i2c/${i}"
    cp -a drivers/media/i2c/${i}/*.h "${pkgdir}/usr/src/linux-${_kernver}/drivers/media/i2c/${i}"
  done

  # add docbook makefile
  install -D -m644 Documentation/DocBook/Makefile \
    "${pkgdir}/usr/src/linux-${_kernver}/Documentation/DocBook/Makefile"

  # add dm headers
  mkdir -p "${pkgdir}/usr/src/linux-${_kernver}/drivers/md"
  cp drivers/md/*.h "${pkgdir}/usr/src/linux-${_kernver}/drivers/md"

  # add inotify.h
  mkdir -p "${pkgdir}/usr/src/linux-${_kernver}/include/linux"
  cp include/linux/inotify.h "${pkgdir}/usr/src/linux-${_kernver}/include/linux/"

  # add wireless headers
  mkdir -p "${pkgdir}/usr/src/linux-${_kernver}/net/mac80211/"
  cp net/mac80211/*.h "${pkgdir}/usr/src/linux-${_kernver}/net/mac80211/"

  # add dvb headers for external modules
  # in reference to:
  # http://bugs.archlinux.org/task/9912
  mkdir -p "${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb-core"
  cp drivers/media/dvb-core/*.h "${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb-core/"
  # and...
  # http://bugs.archlinux.org/task/11194
  #mkdir -p "${pkgdir}/usr/src/linux-${_kernver}/include/config/dvb/"
  #cp include/config/dvb/*.h "${pkgdir}/usr/src/linux-${_kernver}/include/config/dvb/"

  # add dvb headers for http://mcentral.de/hg/~mrec/em28xx-new
  # in reference to:
  # http://bugs.archlinux.org/task/13146
  mkdir -p "${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb-frontends/"
  cp drivers/media/dvb-frontends/lgdt330x.h "${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb-frontends/"
  cp drivers/media/i2c/msp3400-driver.h "${pkgdir}/usr/src/linux-${_kernver}/drivers/media/i2c/"

  # add dvb headers
  # in reference to:
  # http://bugs.archlinux.org/task/20402
  mkdir -p "${pkgdir}/usr/src/linux-${_kernver}/drivers/media/usb/dvb-usb"
  cp drivers/media/usb/dvb-usb/*.h "${pkgdir}/usr/src/linux-${_kernver}/drivers/media/usb/dvb-usb/"
  mkdir -p "${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb-frontends"
  cp drivers/media/dvb-frontends/*.h "${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb-frontends/"
  mkdir -p "${pkgdir}/usr/src/linux-${_kernver}/drivers/media/tuners"
  cp drivers/media/tuners/*.h "${pkgdir}/usr/src/linux-${_kernver}/drivers/media/tuners/"

  # add xfs and shmem for aufs building
  mkdir -p "${pkgdir}/usr/src/linux-${_kernver}/fs/xfs"
  mkdir -p "${pkgdir}/usr/src/linux-${_kernver}/mm"
  cp fs/xfs/xfs_sb.h "${pkgdir}/usr/src/linux-${_kernver}/fs/xfs/xfs_sb.h"

  # fix aufs_type.h
  sed -i "s:__user::g" "${pkgdir}/usr/src/linux-${_kernver}/include/uapi/linux/aufs_type.h"


  # copy in Kconfig files
  for i in `find . -name "Kconfig*"`; do
    mkdir -p "${pkgdir}"/usr/src/linux-${_kernver}/`echo ${i} | sed 's|/Kconfig.*||'`
    cp ${i} "${pkgdir}/usr/src/linux-${_kernver}/${i}"
  done

  chown -R root.root "${pkgdir}/usr/src/linux-${_kernver}"
  find "${pkgdir}/usr/src/linux-${_kernver}" -type d -exec chmod 755 {} \;

  # strip scripts directory
  find "${pkgdir}/usr/src/linux-${_kernver}/scripts" -type f -perm -u+w 2>/dev/null | while read binary ; do
    case "$(file -bi "${binary}")" in
      *application/x-sharedlib*) # Libraries (.so)
        /usr/bin/strip ${STRIP_SHARED} "${binary}";;
      *application/x-archive*) # Libraries (.a)
        /usr/bin/strip ${STRIP_STATIC} "${binary}";;
      *application/x-executable*) # Binaries
        /usr/bin/strip ${STRIP_BINARIES} "${binary}";;
    esac
  done

  # remove unneeded architectures
  rm -rf "${pkgdir}"/usr/src/linux-${_kernver}/arch/{alpha,arm,arm26,avr32,blackfin,c6x,cris,frv,h8300,hexagon,ia64,m32r,m68k,m68knommu,mips,microblaze,mn10300,openrisc,parisc,powerpc,ppc,s390,score,sh,sh64,sparc,sparc64,tile,unicore32,um,v850,xtensa}
}
